package com.educate.book.web.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.educate.common.web.R;
import com.educate.book.model.bo.BookPageBO;
import com.educate.book.model.po.BookPage;
import com.educate.book.rpc.api.BookPageService;
import lombok.extern.log4j.Log4j2;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Log4j2
@RestController
@RequestMapping("bookPage")
public class BookPageController {

    @Reference(version = "${api.service.version}",
            application = "${dubbo.application.id}",
            url = "${dubbo.application.url}")
    private BookPageService bookPageService;

    @PostMapping(value = "/create")
    public R create(@RequestBody BookPageBO bo) {
    	R r = checkPara(bo);
    	if(null!=r)
    		return r;
        Object obj = bookPageService.create(bo);
        return R.ok(obj);
    }

    private R checkPara(BookPage bo) {
    	if(StringUtils.isEmpty(bo.getContentZh()))
    		return R.error("000001", "中文内容不能为空！");
    	if(StringUtils.isEmpty(bo.getContentEn()))
    		return R.error("000002", "英文内容不能为空！");
    	if(StringUtils.isEmpty(bo.getContentEn()))
    		return R.error("000003", "中文单频不能为空！");
    	if(StringUtils.isEmpty(bo.getContentEn()))
    		return R.error("000004", "英文单频不能为空！");
    	if(StringUtils.isEmpty(bo.getPicUrl()))
    		return R.error("000005", "图片不能为空！");
    	if(StringUtils.isEmpty(bo.getPageNum()))
    		return R.error("000006", "页码不能为空！");
    	if(StringUtils.isEmpty(bo.getBookId()))
    		return R.error("000007", "绘本ID不能为空！");
    	return null;
    }
    
    @PostMapping(value = "/create/batch")
    public R create(@RequestBody List<BookPageBO> listBo) {
    	for (BookPageBO bookPageBO : listBo) {
			R r = checkPara(bookPageBO);
			if(r!=null) {
				return r;
			}
		}
        //bookPageService.create(bo);
    	List<String> list = bookPageService.batchCreate(listBo);
        return R.ok(list);
    }
    
    @PostMapping(value = "/delete")
    public R delete(@RequestBody BookPageBO bo) {
    	if(StringUtils.isEmpty(bo.getBookPageId())){
    		return R.error("000001", "必填参数ID不能为空");
    	}
        bookPageService.delete(bo);
        return R.ok();
    }

    @PostMapping(value = "/delete/{bookPageId}")
    public R delete(@PathVariable String bookPageId) {
    	if(StringUtils.isEmpty(bookPageId)){
    		return R.error("000001", "必填参数ID不能为空");
    	}
        bookPageService.delete(bookPageId);
        return R.ok();
    }

    @PostMapping(value = "/detail/{bookPageId}")
    public R<BookPageBO> detail(@PathVariable String bookPageId) {
        BookPageBO bo = bookPageService.detail(bookPageId);
        return R.ok(bo);
    }

    @PostMapping(value = "/detail")
    public R<BookPageBO> detail(@RequestBody BookPageBO bo) {
    	if(StringUtils.isEmpty(bo.getBookPageId())){
    		return R.error("000001", "必填参数ID不能为空");
    	}
        BookPageBO detail = bookPageService.detail(bo);
        return R.ok(detail);
    }

    @PostMapping(value = "/update")
    public R<BookPageBO> update(@RequestBody BookPageBO bo) {
    	if(StringUtils.isEmpty(bo.getBookPageId())){
    		return R.error("000001", "必填参数ID不能为空");
    	}
        bookPageService.update(bo);
        return R.ok();
    }

    @PostMapping(value = "/query")
    public R<Page<BookPageBO>> query(@RequestBody BookPageBO bo) {
        Page<BookPageBO> result = bookPageService.query(bo, bo.pageable());
        return R.ok(result);
    }

    @PostMapping(value = "/list")
    public R<List<BookPageBO>> list(@RequestBody BookPageBO bo) {
        List<BookPageBO> result = bookPageService.list(bo);
        return R.ok(result);
    }

}
